package com.xiaomo.gitee.datamanager.controller.user;

import com.xiaomo.gitee.datamanager.common.result.Code;
import com.xiaomo.gitee.datamanager.common.result.Result;
import com.xiaomo.gitee.datamanager.entity.CClassInfo;
import com.xiaomo.gitee.datamanager.entity.ClassInfo;
import com.xiaomo.gitee.datamanager.service.category.ClassInfoService;
import com.xiaomo.gitee.datamanager.service.category.impl.ClassInfoServiceImpl;
import com.xiaomo.gitee.datamanager.util.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api")
@Tag(name = "数据集及大分类", description = "用户访问数据集和大分类数据")
@RequiredArgsConstructor
public class UserCategoryController {

    private final ClassInfoService classInfoService;

    @Autowired
    public UserCategoryController(ClassInfoServiceImpl classInfoService) {
        this.classInfoService = classInfoService;
    }

    //    @GetMapping("/category/child-classes/{id}")
    @GetMapping("/child-category/{id}")
    @PreAuthorize("hasAuthority('category_info')")
    @Operation(summary = "获取某大类别数据集列表",
            description = "通过大类别ID获取此大类别下的所有数据集",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(name = "id", description = "大类别ID", required = true, in = ParameterIn.PATH)
            },
            responses = {
                @ApiResponse(
                        responseCode = Code.SUCCESS,
                        description = "成功",
                        content = @Content(
                                mediaType = "application/json",
                                array = @ArraySchema(
                                        schema = @Schema(
                                                implementation = CClassInfo.class
                                        )
                                )
                        )
                )
            }
    )
    public Result<List<CClassInfo>> getChildClasses(@PathVariable String id) {
        if (id.equalsIgnoreCase("undefined")) {
            return Result.fail(404,"");
        }
        return Result.ok(classInfoService.getChildClassesByPClass(id));
    }

    @GetMapping("/parent-category")
    @PreAuthorize("hasAuthority('category_info')")
    @Operation(summary = "获取大分类列表",
            description = "获取所有大类别",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            responses = {
                @ApiResponse(
                        responseCode = Code.SUCCESS,
                        description = "成功",
                        content = @Content(
                                array = @ArraySchema(
                                        schema = @Schema(
                                                implementation = ClassInfo.class
                                        )
                                )
                        )
                )
            }
    )
    public Result<List<ClassInfo>> getParentClasses() {
        return Result.ok(classInfoService.getParentClasses());
    }

}
